<?php
	/**
	 * Class representing a query result.
	 * @author IMP
	 */
	
	abstract class DatabaseQueryResult {		
		public final function fetchArrays(){
			$arrays = array();
			
			for($i = 0; $i < $this->getRowCount(); $i++){
				$arrays[] = $this->fetchArray();
			}
			
			return $arrays;
		}
		
		public final function fetchAssocs(){
			$assocs = array();
			
			for($i = 0; $i < $this->getRowCount(); $i++){
				$assocs[] = $this->fetchAssoc();
			}
			
			return $assocs;
		}
		
		public final function fetchObjects(){
			$objs = array();
			
			for($i = 0; $i < $this->getRowCount(); $i++){
				$objs[] = $this->fetchObject();
			}
			
			return $objs;
		}
		
		public final function fetchAssoc(){
			$assoc = $this->__fetchAssoc();
			
			if(is_array($assoc) && count($assoc) >= $this->getFieldCount()){
				return $assoc;
			}else{
				throw new DatabaseException('Wrong implementation of __fetchAssoc in class ' . get_class($this) . '.');
			}
		}
	
		public final function fetchArray(){
			$assoc = $this->__fetchArray();
			
			if(is_array($assoc) && count($assoc) >= $this->getFieldCount()){
				return $assoc;
			}else{
				throw new DatabaseException('Wrong implementation of __fetchArray in class ' . get_class($this) . '.');
			}
		}
		
		public final function fetchObject(){
			$obj = $this->__fetchObject();
			
			if(is_object($obj) && count(get_object_vars($obj)) >= $this->getFieldCount()){
				return $obj;
			}else{
				throw new DatabaseException('Wrong implementation of __fetchObject in class ' . get_class($this) . '.');
			}
		}
		
		public final function getRowCount(){
			$result = $this->__getRowCount();
			
			if(is_int($result)){
				return $result;
			}else{
				throw new DatabaseException('Wrong implementation of __getRowCount in class ' . get_class($this) . '.');
			}
		}
		
		public final function getFieldCount(){
			$result = $this->__getFieldCount();
			
			if(is_int($result)){
				return $result;
			}else{
				throw new DatabaseException('Wrong implementation of __getFieldCount in class ' . get_class($this) . '.');
			}
		}
		
		public final function getFieldType($id){
			$result = $this->__getFieldType($id);
			
			if(!empty($result) && is_string($result)){
				return $result;
			}else{
				throw new DatabaseException('Wrong implementation of __getFieldType in class ' . get_class($this) . '.');
			}
		}
		
		public final function getFieldName($id){
			$result = $this->__getFieldName($id);
			
			if(!empty($result) && is_string($result)){
				return $result;
			}else{
				throw new DatabaseException('Wrong implementation of __getFieldName in class ' . get_class($this) . '.');
			}
		}
		
		public final function getFieldTable($id){
			$result = $this->__getFieldTable($id);
			
			if(!empty($result) && is_string($result)){
				return $result;
			}else{
				throw new DatabaseException('Wrong implementation of __getFieldTable in class ' . get_class($this) . '.');
			}
		}
		
		public final function getFieldLength($id){
			$result = $this->__getFieldLength($id);
			
			if(is_int($result)){
				return $result;
			}else{
				throw new DatabaseException('Wrong implementation of __getFieldLength in class ' . get_class($this) . '.');
			}
		}
		
		public final function getFieldFlags($id){
			$result = $this->____getFieldFlags($id);
			
			if(is_string($result)){
				return $result;
			}else{
				throw new DatabaseException('Wrong implementation of __getFieldFlags in class ' . get_class($this) . '.');
			}
		}
		
		public final function free(){
			return $this->__free() ? true : false;
		}
		
		public final function fetchField($row, $id){
			return $this->__fetchField($row, $id);
		}
		
		abstract protected function __fetchAssoc();
		abstract protected function __fetchArray();
		abstract protected function __fetchObject();
		abstract protected function __fetchField($row, $id);
		abstract protected function __getRowCount();
		abstract protected function __getFieldCount();
		abstract protected function __getFieldType($id);
		abstract protected function __getFieldName($id);
		abstract protected function __getFieldTable($id);
		abstract protected function __getFieldLength($id);
		abstract protected function __getFieldFlags($id);
		abstract protected function __free();
	}
?>